/**
 * 转课，新课程信息
 */
var ChangeCourseDlg = {
    width: '650px',
    height: 'auto',
    cache: "",
    orderItemId: "",       //订单明细ID
    userId: "",             //用户ID
    id: "newCourseTable", //表格id
    me: "",
    countIndex: ""
};

/**
 * 初始化新增弹出框
 */
ChangeCourseDlg.init = function () {
    this.cache = $("#changeCourseDialog").html();
    $("#changeCourseDialog").html('');
};

/**
 * 初始化整个表格
 */
ChangeCourseDlg.initTable = function () {
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(ChangeCourseDlg.id, Feng.ctxPath + "/course/selectEffectiveCourse", defaultColunms);
    bstable.setData({userId: ChangeCourseDlg.userId});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
ChangeCourseDlg.initColumn = function () {
    var columns = [
        {
            title: '序号', align: 'center', valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index + 1;
            }
        },
        {title: '课程名称', field: 'courseName', align: 'center', valign: 'middle'},
        {
            title: '课程季', field: 'courseSeason', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value === 'spring') {
                    return "春季班";
                } else if (value === 'summer') {
                    return "暑假班";
                } else if (value === 'autumn') {
                    return "秋季班";
                } else if (value === 'winter') {
                    return "寒假班";
                }
            }
        },
        {title: '主讲教师', field: 'teacherMain', align: 'center', valign: 'middle'},
        {title: '课程单价', field: 'coursePrice', align: 'center', valign: 'middle'},
        {title: '课次', field: 'totalNumber', align: 'center', valign: 'middle'},
        {title: '限报人数', field: 'planClassPersons', align: 'center', valign: 'middle'},
        {title: '开课日期', field: 'courseStartDate', align: 'center', valign: 'middle'},
        {
            title: '操作', field: '#', align: 'center',
            formatter: function (value, row, index) {
                return '<button class="btn btn-primary btn-xs" onclick="ChangeCourseDlg.changeCourse(\'' + row.id + '\')" title="转入"><i class="glyphicon glyphicon-link"></i>转入</button> &nbsp;&nbsp;';
            }
        }];
    return columns;
};

/**
 * 点击搜索按钮
 */
ChangeCourseDlg.clickSearch = function () {
    var queryData = {};
    var val_search_courseName = $("#txt_search_courseName").val();
    if (val_search_courseName !== null || val_search_courseName !== "") {
        queryData['courseName'] = val_search_courseName;
    }
    var val_search_courseSeason = $("#txt_search_courseSeason").val();
    if (val_search_courseSeason !== null || val_search_courseSeason !== "") {
        queryData['courseSeason'] = val_search_courseSeason;
    }
    queryData['userId'] = ChangeCourseDlg.userId;
    ChangeCourseDlg.me.refresh({query: queryData});
};

/**
 * 清空搜索栏
 */
ChangeCourseDlg.resetSearchVal = function () {
    //课程名
    $("#txt_search_courseName").val("");
    //课程季
    $("#txt_search_courseSeason").val("");
};

/**
 * 关闭此对话框
 */
ChangeCourseDlg.close = function () {
    layer.close(this.currentLayerIndex);
};
/**
 * 转班
 */
ChangeCourseDlg.changeCourse = function (courseId) {
    layer.prompt({
        title: '输入转入次数，并确认',
        formType: 0
    }, function (value, index) {
        layer.close(index);
        //发送转课请求
        var option = {
            orderItemId: ChangeCourseDlg.orderItemId,
            courseId: courseId,
            purchaseTimes: value
        };
        var ajax = new $ax(Feng.ctxPath + "/order/changeCourse", function (data) {
            if (data.success) {
                Feng.success("转课成功!");
                ChangeCourseDlg.close();
                OrderInfoDlg.close();
            } else if (data.code === 2) {
                Feng.error(data.msg);
            } else {
                Feng.error("转课失败!");
            }
        }, function (data) {
            Feng.error("转课失败!");
        });
        ajax.setData(option);
        ajax.start();
    });

};
